// Loggr::Tools::Guard
// Tools make Loggr be more autonomous.
// By George Ulyanov (c) 2010

#include "Config.h"

#ifndef LOGGR_TOOLS_GUARD_H_
#   include "Guard.h"
#endif

#if defined (LOGGR_WIN32API)
#	include <Windows.h>
#elif defined (LOGGR_OTHER)
#	include <stdlib.h>
#endif
#include <stdexcept>

namespace Loggr
{
    namespace Tools
    {
        struct GuardData
        {
#if defined (LOGGR_WIN32API)
            CRITICAL_SECTION    m_critSec;
#elif defined (LOGGR_OTHER)
#   pragma error "Not implemented"
#endif
        };

        Guard::Guard()
        {
            m_data = new GuardData();
            if (!m_data)
            {
                throw std::runtime_error("Couldn't initialize Guard::GuardData.");
            }
#if defined (LOGGR_WIN32API)
            ::InitializeCriticalSection(&m_data->m_critSec);
#elif defined (LOGGR_OTHER)
#   pragma error "Not implemented"
#endif
        }

        Guard::~Guard() throw()
        {
#if defined (LOGGR_WIN32API)
            ::DeleteCriticalSection(&m_data->m_critSec);
#elif defined (LOGGR_OTHER)
#   pragma error "Not implemented"
#endif
            delete m_data;
        }

        void Guard::Lock()
        {
#if defined (LOGGR_WIN32API)
            ::EnterCriticalSection(&m_data->m_critSec);
#elif defined (LOGGR_OTHER)
#   pragma error "Not implemented"
#endif
        }

        void Guard::Unlock()
        {
#if defined (LOGGR_WIN32API)
            ::LeaveCriticalSection(&m_data->m_critSec);
#elif defined (LOGGR_OTHER)
#   pragma error "Not implemented"
#endif
        }
    }
    // namespace Tools
}
// namespace Loggr
